package com.ren.article.controller;


import com.ren.article.entity.ReclArticle;
import com.ren.article.entity.vo.ReclArticleQueryVo;
import com.ren.article.entity.vo.ReclArticleVo;
import com.ren.article.service.ReclArticleService;
import com.ren.utils.page.PageItem;
import com.ren.utils.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author RZY
 * @since 2022-07-20
 */

@Api(tags = "美文接口")
@RestController
@RequestMapping("/recl-article")
public class ReclArticleController {

    @Autowired
    ReclArticleService articleService;

    @ApiOperation(value = "查询指定id的美文数据")
    @GetMapping("/{id}")
    public R getArticleById(@PathVariable Long id) {
        ReclArticleVo article = articleService.getArticleVoById(id);
        return R.ok()
                .data("article", article);
    }

    @ApiOperation(value = "查询美文数据分页")
    @PostMapping("/{current}/{limit}")
    public R getArticleOfPage(@RequestBody(required = false) ReclArticleQueryVo reclArticleQueryVo,
                            @PathVariable long current,
                            @PathVariable long limit) {
        PageItem<ReclArticleVo> articlePageItem = articleService.getArticleOfPage(reclArticleQueryVo, current, limit);
        return R.ok()
                .data("records", articlePageItem.getPageRecords())
                .data("total", articlePageItem.getTotal());
    }

    @ApiOperation(value ="修改指定id的美文数据")
    @PutMapping("")
    public R updateArticleById(@RequestBody ReclArticle reclArticle) {
        boolean res = articleService.updateArticleById(reclArticle);
        if(res) return R.ok()
                .message("美文修改成功！");
        else return R.error()
                .message("美文修改失败，请稍后再试！");
    }

    @ApiOperation(value = "删除指定id的美文数据")
    @DeleteMapping("/{id}")
    public R delArticleById(@PathVariable Long id) {
        boolean res = articleService.delArticleById(id);
        if(res) return R.ok()
                .message("美文删除成功！");
        else return R.error()
                .message("美文删除失败，请稍后再试！");
    }

    @ApiOperation(value = "添加美文数据")
    @PostMapping("")
    public R addArticleById(@RequestBody ReclArticle reclArticle) {
        boolean res = articleService.addArticle(reclArticle);
        if(res) return R.ok()
                .message("美文添加成功！");
        else return R.error()
                .message("美文添加失败，请稍后再试！");
    }
}

